<?php

error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('max_execution_time', 120);

setlocale(LC_CTYPE, array("ru_RU.UTF-8","ru_RU","russian","ru_SU","ru"));
function assert_callcack($file, $line, $message)
{
	error_log("ASSERT: $message in $file on line $line");
}

assert_options(ASSERT_ACTIVE,     1);
assert_options(ASSERT_WARNING,    0);
assert_options(ASSERT_BAIL,       0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK,   'assert_callcack');

$_base_dir = url_dirname(__FILE__, 2);
$_lib_dir = $_base_dir.'admin/lib/';

ini_set('include_path', $_lib_dir.PATH_SEPARATOR.$_base_dir.'lib/'.PATH_SEPARATOR.ini_get('include_path'));

require_once $_lib_dir.'request.php';

function url_dirname($path, $level=1)
{
	//$path = preg_replace('#\\\\+|/+#', '/', $path);
	while ($level-- > 0) $path = dirname($path);
	if (strpos($path, '\\') !== false) $path = str_replace('\\', '/', $path);
	return $path === '/' ? $path : $path . '/';
}

function disp_html ($env)
{
	global $HTML;
	$tpl = tpl_engine();
	$tpl->load($env);
	$html = isset($HTML) ? $HTML : 'html.html';
	echo $tpl->parse($html);
} // end of function disp_html


$start = microtime(true);
define ('RH_module', 1);

$RH_ATF=array(
	'Default' => array(
		'go' => url_dirname(__FILE__).'site.php',
	),
);

$RH_STATE = 'Default';
$RH_EVENT = 'go';

// для запуска обработчиков в глобальном пространстве имен это не функция

while (isset($RH_STATE)) {
	$RH_NEXT_STATE = $RH_NEXT_EVENT = NULL;
	$action = $RH_ATF[$RH_STATE][$RH_EVENT];
	if (is_callable($action)) $env = call_user_func($action);
	else $env = require $action;
	if ($env !== false && $env !== 1) disp_html($env);
	$RH_STATE = $RH_NEXT_STATE;
	$RH_EVENT = isset($RH_NEXT_EVENT) ? $RH_NEXT_EVENT : $RH_EVENT;
}

echo sprintf("<!-- %.4f -->\n", microtime(true) - $start);
